
var layer = layui.layer //弹层
    ,table = layui.table //表格
form = layui.form,$ = layui.jquery,laydate = layui.laydate,purchaseId= getQueryVariable("purchaseId");
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});

let initData=[];
layer.ready(function () {
    $.ajax({
        async: false,
        type: 'POST',
        url: 'purchase/selectProductByPuId/'+purchaseId,
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        success: function (res) {
            if (res.status == 200) {
               initData = res.data;

               if (res.data){
                   for(let i in res.data){
                       $.ajax({
                           type:"POST",
                           dataType:"json",
                           contentType: 'application/json',
                           data: {purchaseProductId:res.data[i].id,purchaseId:res.data[i].purchaseId},
                           url:"purchase/getFeedbackDateByParam",
                           async: false,
                           success: function (data) {
                               if (data.data){
                                   for(let j in data.data){
                                       for (let k in initData){
                                           if (initData[k].id===data.data[j].purchaseProductId && initData[k].purchaseId===data.data[j].purchaseId){
                                               initData[k].feedbackDeliveryDate=data.data[j].feedbackDeliveryDateStr
                                               initData[k].actualDeliveryDate=data.data[j].actualDeliveryStr
                                               initData[k].description=data.data[j].description
                                           }
                                       }

                                   }
                               }
                           }
                       });
                   }
               }



            }
        },
        error: function (data) {
            layer.msg("产品初始化异常",{icon: 5});
        }
    });

});
table.render({
    elem: '#mains'
    ,data:initData
    ,width: 1400
    ,height:600
    ,toolbar:'#realToolbar'
    ,cols: [[
        ,{field:'id', hide:true}//采购产品id
        ,{field:'purchaseId', hide:true}
        ,{field:'salesContractNo', title:'销售合同号'}
        ,{field:'productNo', title: '产品编码'}
        ,{field:'specNo', title:'规格编码'}
        ,{field:'amount', title:'数量'}
        ,{field:'feedbackDeliveryDate', title:'供应商反馈交期',edit: true,event: 'feedbackDeliveryDate'}
        ,{field:'actualDeliveryDate', title:'实际交期',edit: true,event: 'actualDeliveryDate'}
        ,{field:'description', title:'备注',edit: true}
    ]]
    ,id: 'mainstableid'
    ,limit: initData.length
});


//只有点击的时候才会进入这个里面
table.on('tool(mainstable)', function (d) {
    var data = d.data;
    var newdata = {};
    switch (d.event) {
        case "actualDeliveryDate":
            var field = $(this).data('field');
            laydate.render({
                elem: this.firstChild // 固定写
                , show: true //直接显示
                , closeStop: this
                , type: 'date'
                , format: "yyyy-MM-dd"
                , done: function (value, date) {
                    newdata[field] = value;
                    d.update(newdata);
                    setTimeactualDeliveryDate(data, value);
                }
            });
       break;
        case "feedbackDeliveryDate":
            var field = $(this).data('field');
            laydate.render({
                elem: this.firstChild // 固定写
                , show: true //直接显示
                , closeStop: this
                , type: 'date'
                , format: "yyyy-MM-dd"
                , done: function (value, date) {
                    newdata[field] = value;
                    d.update(newdata);
                    setTimefeedbackDeliveryDate(data, value);
                }
            });
            break;
    }
});
function setTimeactualDeliveryDate(data, val){
    let tableData = table.cache["mainstableid"];
    for(let i in tableData){
        if(data.id == tableData[i].id){
            tableData[i].actualDeliveryDate = val;
        }
    }
}
function setTimefeedbackDeliveryDate(data, val){
    let tableData = table.cache["mainstableid"];
    for(let i in tableData){
        if(data.id == tableData[i].id){
            tableData[i].feedbackDeliveryDate = val;
        }
    }
}

$("#choose").on("click",function () {
    let data =  table.cache["mainstableid"];
    for (let i in data) {
        data[i].purchaseProductId=data[i].id;
        data[i].id=null
    }
    $.ajax({
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:JSON.stringify(data),
        url:"purchase/addFeedbackDate",
        success:function (res) {
            if (res.status == 200) {
                //执行接口
                layer.msg("保存成功", {icon: 6});
                setTimeout(function () {
                    //保存成功则关闭此页面
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                },1000);
                return false;
            } else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });

});




$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})